package cn.net.xyan.yypt

import cn.net.xyan.blossom.kotlin.AllOpen
import cn.net.xyan.yypt.service.*
import org.apache.cxf.Bus
import org.apache.cxf.jaxws.EndpointImpl
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean
import org.springframework.boot.autoconfigure.domain.EntityScan
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.ComponentScan
import org.springframework.context.annotation.Configuration
import org.springframework.data.jpa.repository.config.EnableJpaRepositories
import org.springframework.scheduling.annotation.EnableAsync
import java.lang.reflect.Proxy
import javax.xml.ws.Endpoint

@AllOpen
@Configuration
@EntityScan
@ComponentScan
@EnableJpaRepositories(basePackages = arrayOf("cn.net.xyan.yypt.dao"))
@EnableAsync
class YDPTConfiguration{
    @Autowired
    lateinit var  bus: Bus

    @Bean
    fun endpoint(payOrderService: PayOrderService): Endpoint {
        val endpoint = EndpointImpl(bus, payOrderService)
        endpoint.publish("/pay")
        return endpoint
    }

    @Bean
    fun payOrderService(): PayOrderService {
        return PayOrderServiceImpl()
    }

    @Bean
    fun linkingCloudOpenApiInvocationHandler(): LinkingCloudOpenApiInvocationHandler {
        return LinkingCloudOpenApiInvocationHandler()
    }

    @Bean
    fun linkingCloudOpenApi(invocationHandler: LinkingCloudOpenApiInvocationHandler): LinkingCloudOpenApi {
        return Proxy.newProxyInstance(YDPTConfiguration::class.java.classLoader,
                arrayOf(LinkingCloudOpenApi::class.java), invocationHandler) as LinkingCloudOpenApi
    }

    @Bean
    @ConditionalOnMissingBean
    fun defaultPayOrderCallbackService(): PayOrderCallbackService{
        return DefaultCallbackService()
    }
}